package GOL;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class Game extends JPanel implements MouseListener{
	
    	Boolean[][] board;
		JFrame frame = new JFrame();
		JButton[][] grid; 
		Color on = new Color(200,50,20);
		Color off = new Color(250,250,250);
		
		
		public Game(int a, int b) {
			
			board = new Boolean[a][b];
			for (int i=0; i <a ;i++){
				for (int j=0; j<b;j++){
					board[i][j] = false;
				}}
			
			  frame.setLayout(new GridLayout(a,b)); 
              grid=new JButton[a][b]; 
              for(int x=0; x<a; x++){
                      for(int y=0; y<b; y++){
                    	  grid[x][y]=new JButton(); //creates new button    
                    	  grid[x][y].addMouseListener(this);
                    	  grid[x][y].setBackground(off);
                    	  grid[x][y].setBorderPainted(false);
                          frame.add(grid[x][y]); //adds button to grid
                      }
              }
              frame.setSize(1000, 800);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true); //makes frame visible
			
		}
		


		public void mouseClicked(MouseEvent evt) {
	      switch (evt.getModifiers())  {
	      case 16:    
            for(int x=0; x<grid.length; x++){
          	  for(int y=0; y<grid[0].length; y++){
          
				JButton btn = grid[x][y];
				if (evt.getSource() == btn) {
					ButtonClicked(x, y);
					evt.consume();
					break;}
          	  }}
            break;
	      case 8: switchAuto(); 
	      		  break;
	      case 4: step();}
		}

		public void redraw(){
            for(int x=0; x<grid.length; x++){
                for(int y=0; y<grid[0].length; y++){
              	  grid[x][y].setBackground((board[x][y])? on : off);
                }
        }
        frame.repaint();
		}
		
		
		public void ButtonClicked(int x, int y){
			switchEntry(x, y);
			redraw();
		}
		
		Boolean auto = false;

		
		public String showBoard(){
			String Bstr = "";
			for (int i=0; i <board.length ;i++){
				for (int j=0; j<board[i].length;j++){
					Bstr = Bstr + board[i][j].toString() + '\\';
				} Bstr = Bstr + '\n';
			}
			
			return Bstr;
		}
		
		
		public Boolean check(Integer a, Integer b){
		   int xi = (a == 0)? 0 : a-1;
		   int yi = (b == 0)? 0 : b-1;
		   int xa = (a == board.length -1)? board.length-1 : a+1;
		   int ya = (b == board[0].length -1)? board[0].length-1 : b+1;
		   int count = board[a][b]? -0 : 0;
		   
			for (int i=xi; i <=xa ;i++){
				for (int j=yi; j<=ya ;j++){  
				if (board[i][j]) count++;
				}}
		  return (2<count&&count<5);	
		}
		
		
		public void step(){
			Boolean[][] nboard = new Boolean[board.length][board[0].length];
			for (int i=0; i <board.length ;i++){
				for (int j=0; j<board[i].length;j++){	
				nboard[i][j]=check(i,j);	
				}}
			board = nboard;
			redraw();	
			
			
			if (auto) 
				new Thread() {
				public void run() {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					step();		
				}	
			}.start();
			
		}
		
		public void switchEntry(int x, int y){
			board[x][y] = !board[x][y];
		}
		public void switchAuto(){
			auto= !auto;
		}
		
		@Override
		public void mouseEntered(MouseEvent arg0) {
		}


		@Override
		public void mouseExited(MouseEvent arg0) {			
		}


		@Override
		public void mousePressed(MouseEvent arg0) {			
		}


		@Override
		public void mouseReleased(MouseEvent arg0) {			
		}
		
		
		public static void main(String[] args){
			new Game(10,13);
			}
		
		
}
